/* author: Mariana Horita
   mariana.horita@gmail.com
   file: Resolucao.cs*/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml.Linq;

namespace Resolucao
{
    public partial class Resolucao : Form
    {

        private string caminho = @"..\..\fonte.xml";
        private XElement _ExercicioRepository;
        private List<Aula> aulas;
        private Exercicio lastSelecteExercise;


        public Resolucao()
        {
            InitializeComponent();
            MessageBox.Show("Implementação dos exercícios propostos em aula.\n\nLembre-se que há várias formas de solucionar um mesmo exercício, e esta é apenas uma forma.");

            _ExercicioRepository = XElement.Load(caminho);

            aulas = LoadInfo();

            LoadTreeView();

           
        }


        private List<Aula> LoadInfo()
        {


            IEnumerable<XElement> query =
                    from aula in _ExercicioRepository.Elements("aula")
                    select aula;

            aulas = new List<Aula>();

            foreach (XElement aula in query)
            {
                Aula aul = new Aula();
                aul.Id = Convert.ToInt32(aula.Element("id").Value);
                aul.Exercise = new List<Exercicio>();

                
                foreach (XElement exercicio in (from ex in aula.Elements("exercicio") select ex) )
                {
                    Exercicio exerc = new Exercicio();
                    exerc.Number = Convert.ToInt32(exercicio.Element("numero").Value);
                    exerc.Enunciado = exercicio.Element("enunciado").Value;
                    exerc.AulaId = aul.Id;
                    aul.Exercise.Add(exerc);
                }

                aulas.Add(aul);

            }
            return aulas;
        }

        private void LoadTreeView()
        {
            foreach (Aula a in aulas)
            {
                trvAula.NodeMouseClick += new TreeNodeMouseClickEventHandler(trvAula_NodeMouseClick);

                TreeNode tn = trvAula.Nodes.Add("Aula " + a.Id.ToString());

                foreach (Exercicio e in a.Exercise)
                {
                    tn.Nodes.Add("Exercício " + e.Number);
                }
            }
        }

        void trvAula_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            ShowEnunciado(e.Node);
        }

        private void trvAula_AfterSelect(object sender, TreeViewEventArgs e)
        {
            ShowEnunciado(e.Node);
        }

        private void ShowEnunciado(TreeNode currentNode)
        {
            if (currentNode.Parent != null)
            {


                Exercicio exerc = (from ex in
                                       (from a in aulas
                                        where a.Id.ToString() == currentNode.Parent.Text.Replace("Aula ", "")
                                        select a.Exercise).Single()
                                   where ex.Number.ToString() == currentNode.Text.Replace("Exercício ", "")
                                   select ex).Single();

                txtEnunciado.Text = exerc.Enunciado;

                lastSelecteExercise = exerc;

                btIr.Enabled = true;
            }
            else
            {
                txtEnunciado.Clear();
                btIr.Enabled = false;
            }
        }

        private void btIr_Click(object sender, EventArgs e)
        {
            Form f = new Form();
            switch (lastSelecteExercise.AulaId)
            {
                #region Aula 2
                case 2:
                    switch (lastSelecteExercise.Number)
                    {
                        case 1:
                            f = new A2E1();
                            break;
                        case 2:
                            f = new A2E2();
                            break;
                        case 3:
                            f = new A2E3();
                            break;
                        case 4:
                            f = new A2E4();
                            break;
                        case 5:
                            f = new A2E5();
                            break;
                        case 6:
                            f = new A2E6();
                            break;
                    }
                    break;
                #endregion Aula 2
                #region Aula 3
                case 3:
                    switch (lastSelecteExercise.Number)
                    {
                        case 1:
                            f = new A3E1();
                            break;
                        case 2:
                            f = new A3E2();
                            break;
                        case 3:
                            f = new A3E3();
                            break;
                        case 4:
                            f = new A3E4();
                            break;
                        case 5:
                            f = new A3E5();
                            break;
                    }
                    break;
                #endregion Aula 3
                #region Aula 4
                case 4:
                    switch (lastSelecteExercise.Number)
                    {
                        case 1:
                            f = new A4E1();
                            break;
                        case 2:
                            f = new A4E2();
                            break;
                        case 3:
                            f = new A4E3();
                            break;
                        case 4:
                            //f = new A4E4();
                            break;
                        case 5:
                            //f = new A4E5();
                            break;
                    }
                    break;
                #endregion Aula 4
            }
            f.ShowDialog();
        }

        

        

    }
}
